<?php

use Mapper\MemberModel as MemberMapper;

class MemberController extends \Base\AbstractController {


    /**
     * @desc 列表页
     */
    public function listAction() {
        $request = $this->getRequest();
        $class   = $request->get('class');
        $keyword = trim($request->get('keyword'));
        $gender  = (int)$request->get('gender');
        $where   = [];
        $return  = [];

        if(!empty($keyword) && !empty($class)) {

            switch($class) {
                case 1  :
                    $first = ['id' => $keyword];
                    break;
                case 2  :
                    $first = ['nick_name LIKE "%' . urlencode($keyword) . '%"'];
                    break;
                case 3  :
                    $first = ['mobile LIKE "%' . $keyword . '%"'];
                    break;
                default :
                    break;
            }
        }

        if(!empty($gender)) {
            $second = ['gender' => $gender];
        }

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second);
        } elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = $first : $where = $second;
        }

        $page = new \Lib\Paginator('member');
        $page->where($where);
        $this->assign('page', $page->getPage());
        $this->assign('count', $page->count());
        $this->assign('perPage', $page->getPerpage());
        $listModels   = $page->lists();
        $memberSwitch = Mapper\MemberSwitchModel::getInstance();

        foreach($listModels as $listModel) {
            $listData                   = $listModel->toArray();
            $authentication             = $memberSwitch->findByUid($listData['id']);
            $listData['authentication'] = $authentication;
            $listData['nick_name']      = urldecode($listData['nick_name']);
            $return[]                   = $listData;
        }
        $this->assign('lists', $return);
    }

    /**
     * @desc 编辑页
     */
    public function editAction() {
        $request           = $this->getRequest();
        $id                = (int)$request->get('id');
        $memberMapper      = MemberMapper::getInstance();
        $data              = $memberMapper->findById($id)->toArray();
        $avatarMapper      = Mapper\MemberAvatarModel::getInstance();
        $data['avatar']    = array_values($avatarMapper->avatars($id));
        $data['nick_name'] = urldecode($data['nick_name']);

        $this->assign('data', $data);
    }

    /**
     * @desc 屏蔽
    */
    public function delAction() {
        $request = $this->getRequest();
        $id = $request->get('id');
        $status = $request->get('status');

        $memberMapper = MemberMapper::getInstance();

        if($status == 1) {
            $temp = 2;
        }elseif($status == 2) {
            $temp = 1;
        }

        try{
            $memberMapper->update(['status'=>$temp],['id'=>$id]);
            return \Response::msg('操作成功');
        }catch (Exception $exc) {
            return \Response::errmsg($exc->getMessage());
        }

    }
}
